
#include "TimeList.h"
#include "Config.h"

TimeListDef TimeList;

sTimeListWorkDef *SysTimeList;

void sTimeListInit(void)
{	
	SysTimeList = &SetDataConfig->sTimeList;
	SysTimeList->TimeListSizeMax = TIME_LIST_MAX;
}

void SysTimeProcess(void)
{
	if(TimeList.tTime != SysTimeUnix)
	{
		{//当前天已经经过的分钟数
			TimeList.tTime = SysTimeUnix;
			TimeList.NowMinutes = (TimeList.tTime%(60*60*24))/60;
			UnixToDateTime(TimeList.tTime, (DateTimeDef *)&TimeList.tDate);
		}
		if(0!=tTimeSart) //设备存储的时间是否校准完成
		{
			/***************   循环处理定时任务  BEGEN******************************/
			for(uint8_t i = 0; i < TIME_LIST_MAX; i++)
			{
				/***************   定时任务ID非0，Enable非0，定时有效    ******************************/
				if((0 != SysTimeList->TimeList[i].Id)&&(0 != SysTimeList->TimeList[i].Enable))
				{
					/***************   定时任务启动时间≠停止时间，定时有效    ******************************/
					TimeList.OnUnixTime[i]  = (SysTimeList->TimeList[i].onHours*60+SysTimeList->TimeList[i].onMinutes)%(24*60*60);		//定时任务 控制开启时间(秒)
					TimeList.OffUnixTime[i] = (SysTimeList->TimeList[i].offHours*60+SysTimeList->TimeList[i].offMinutes)%(24*60*60);	//定时任务 控制关闭时间(秒)
					
					//启动时间≠停止时间；当前时间大于启动时间；当前时间小于停止时间；星期使能有效=定时有效，执行定时
					if((TimeList.OnUnixTime[i] != TimeList.OffUnixTime[i]))
					{
						if((0 == TimeList.TimeListisWork[i])
								&&(((TimeList.OnUnixTime[i] < TimeList.OffUnixTime[i])&&(TimeList.NowMinutes >= TimeList.OnUnixTime[i])&&(TimeList.NowMinutes < TimeList.OffUnixTime[i]))
								||((TimeList.OnUnixTime[i] > TimeList.OffUnixTime[i])&&((TimeList.NowMinutes >= TimeList.OnUnixTime[i])||(TimeList.NowMinutes < TimeList.OffUnixTime[i]))))
								&&(((SysTimeList->TimeList[i].Week)&(1<<TimeList.tDate.WeekDay))||(0x80 == SysTimeList->TimeList[i].Week)))
						{
							RunDataConfig->DO = 1;	//继电器状态置1
							TimeList.TimeListisWork[i]=1;
						}
						else if((0 != TimeList.TimeListisWork[i])
								&&(((TimeList.OnUnixTime[i] < TimeList.OffUnixTime[i])&&((TimeList.NowMinutes < TimeList.OnUnixTime[i])||(TimeList.NowMinutes >= TimeList.OffUnixTime[i])))
								||((TimeList.OnUnixTime[i] > TimeList.OffUnixTime[i])&&((TimeList.NowMinutes < TimeList.OnUnixTime[i])&&(TimeList.NowMinutes >= TimeList.OffUnixTime[i]))))
								&&(((SysTimeList->TimeList[i].Week)&(1<<TimeList.tDate.WeekDay))||(0x80 == SysTimeList->TimeList[i].Week)))
						{
							RunDataConfig->DO = 0;	//继电器状态置0
							TimeList.TimeListisWork[i] = 0;
							if((0x80 == SysTimeList->TimeList[i].Week))
							{//当前定时任务只执行一次
								SysTimeList->TimeList[i].Enable = 0; //执行完成后将定时任务状态置0
							}
						}
					}
				}
			}
		}
		/***************   定时任务处理  END  ******************************/
	}
}
